package org.sgroup.imenu.controladapter;

import java.util.List;
import java.util.Random;
import org.sgroup.imenu.IMenuActivity;
import org.sgroup.imenu.R;
import org.sgroup.imenu.bo.Item;
import org.sgroup.imenu.util.CData;
import org.sgroup.imenu.util.LoadBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OrderItemListAdapter extends ArrayAdapter<Item> {
	private Context mContext;
	private int mViewid;
	private List<Item> mItems;

	public OrderItemListAdapter(Context context, int viewId,
			List<Item> items) {
		super(context, viewId, items);

		this.mContext = context;
		this.mViewid = viewId;
		this.mItems = items;
	}	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater flater = LayoutInflater.from(mContext);
			convertView = flater.inflate(mViewid, null);
		}
		
		ImageButton btnThumbnail = (ImageButton) convertView.findViewById(R.id.item_thumbnail);
		TextView tvItemName = (TextView) convertView.findViewById(R.id.item_name);
		TextView tvItemDesc = (TextView) convertView.findViewById(R.id.item_desc);
		TextView tvItemPrice = (TextView) convertView.findViewById(R.id.item_price);
		TextView tvItemTime = (TextView) convertView.findViewById(R.id.item_time);
		Button btnRemoveCart = (Button) convertView.findViewById(R.id.btnRemoveCart);
		Button btnQuantity = (Button) convertView.findViewById(R.id.btnQuantity);
		
		// get current item
		Item currentItem = mItems.get(position);				
		
		tvItemName.setText(currentItem.Name);
		tvItemDesc.setText(currentItem.Description);
		tvItemPrice.setText(CData.FormatPrice(currentItem.Price));
//		Random randTime = new Random();		
//		tvItemTime.setText(String.valueOf(15 + randTime.nextInt(30)));
		tvItemTime.setText(String.valueOf(currentItem.Time));
		btnQuantity.setText(Integer.toString(currentItem.Quantity));
		if (!currentItem.ImageFile.isEmpty()) {
			try {				
				Bitmap bm= LoadBitmap.GetBitmap(this.mContext,currentItem.ImageFile);				
				btnThumbnail.setImageBitmap(bm);
			} catch (Exception e) {
				Log.v("OrderItemListAdapter", e.getMessage());
			}
		}		
		
		// click button btnRemoveCart
		btnRemoveCart.setTag(position);
		btnRemoveCart.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Button btn = (Button)v;
				int pos = (Integer)btn.getTag();
				Item currentItem = mItems.get(pos);
																
				((IMenuActivity)mContext).RemoveOrder(currentItem);	
																																								
				if(currentItem.Quantity > 0)
				{
					// update TextView "quantity"
					RelativeLayout relLayout = (RelativeLayout)btn.getParent();				
					Button btnQuantity = (Button) relLayout.findViewById(R.id.btnQuantity);
					btnQuantity.setText(String.valueOf(currentItem.Quantity));										
				}
				else
				{
					// update ListView after removing item from adapter
					notifyDataSetChanged();
				}
				
			}
		});
		
		return convertView;
	}
	
	
}
